package com.bh.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.wms.domain.WmsSku;
import com.bh.wms.mapper.WmsSkuMapper;
import com.bh.wms.service.IWmsSkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * SKU服务实现
 *
 * @author GPT
 */
@Service
public class WmsSkuServiceImpl extends ServiceImpl<WmsSkuMapper, WmsSku> implements IWmsSkuService {

    @Override
    public WmsSku selectWmsSkuById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public WmsSku selectWmsSkuByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        return baseMapper.selectOne(new LambdaQueryWrapper<WmsSku>()
                .eq(WmsSku::getCode, code)
                .last("limit 1"));
    }

    @Override
    public List<WmsSku> selectWmsSkuList(WmsSku query) {
        LambdaQueryWrapper<WmsSku> wrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            if (StringUtils.isNotEmpty(query.getCode())) {
                wrapper.eq(WmsSku::getCode, query.getCode());
            }
            if (StringUtils.isNotEmpty(query.getName())) {
                wrapper.like(WmsSku::getName, query.getName());
            }
            if (query.getStatus() != null) {
                wrapper.eq(WmsSku::getStatus, query.getStatus());
            }
            if (StringUtils.isNotEmpty(query.getSkuType())) {
                wrapper.eq(WmsSku::getSkuType, query.getSkuType());
            }
        }
        wrapper.orderByDesc(WmsSku::getCreateTime);
        return baseMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsSku(WmsSku entity) {
        if (StringUtils.isEmpty(entity.getCode())) {
            throw new ServiceException("请填写SKU编码");
        }
        WmsSku exist = selectWmsSkuByCode(entity.getCode());
        if (exist != null) {
            throw new ServiceException("SKU编码已存在");
        }
        fillCreateInfo(entity);
        return baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsSku(WmsSku entity) {
        if (StringUtils.isEmpty(entity.getCode())) {
            throw new ServiceException("请填写SKU编码");
        }
        WmsSku exist = selectWmsSkuByCode(entity.getCode());
        if (exist != null && !exist.getId().equals(entity.getId())) {
            throw new ServiceException("SKU编码已存在");
        }
        fillUpdateInfo(entity);
        return baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsSkuByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsSkuById(Long id) {
        return baseMapper.deleteById(id);
    }

    private void fillCreateInfo(WmsSku entity) {
        Date now = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        if (entity.getStatus() == null) {
            entity.setStatus(1);
        }
        entity.setCreateName(username);
        entity.setCreateTime(now);
        entity.setModifyName(username);
        entity.setModifyTime(now);
    }

    private void fillUpdateInfo(WmsSku entity) {
        entity.setModifyName(SecurityUtils.getUsername());
        entity.setModifyTime(DateUtils.getNowDate());
    }
}

